using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    class CompareEqual : Component 
    {

        public NodeVector InputNodesA;
        public NodeVector InputNodesB;
        public NodeVector Output;
        
		public CompareEqual(Component poParentComponent, string psName, int piWidthA, int piWidthB) 
            : base (poParentComponent,psName)
		{
            InputNodesA = this.CreateNodeVector("InA", piWidthA);
            InputNodesB = this.CreateNodeVector("InB", piWidthB);
            Output = this.CreateNodeVector("Out", 1);
		}

        public override void GenerateStructure()
        {
            int iXnors = InputNodesA.Nodes.Count;
            if (iXnors > InputNodesB.Nodes.Count)
                iXnors = InputNodesB.Nodes.Count;



            AndGate oAndGate = new AndGate(this, "and_all", Utility.WidestNodeVector(InputNodesA, InputNodesB));

            for (int iBit = 0; iBit < iXnors; iBit++)
            {
                Xnor oXnor = new Xnor(this, "xnor" + iBit.ToString());
                oXnor.InputNodes[0] = InputNodesA[iBit];
                oXnor.InputNodes[1] = InputNodesB[iBit];
                oXnor.OutputNodes[0] = oAndGate.InputNodes[iBit];
            }

            if (iXnors < InputNodesA.Nodes.Count)
            {
                for (int iBit = iXnors; iBit < InputNodesA.Nodes.Count; iBit++)
                {
                    Not oNot = new Not(this,"not" + iBit.ToString());
                    oNot.InputNodes[0] = InputNodesA[iBit];
                    oNot.OutputNodes[0] = oAndGate.InputNodes[iBit];
                }
            }

            if (iXnors < InputNodesB.Nodes.Count)
            {
                for (int iBit = 0; iBit < InputNodesB.Nodes.Count; iBit++)
                {
                    Not oNot = new Not(this, "not" + iBit.ToString());
                    oNot.InputNodes[0] = InputNodesB[iBit];
                    oNot.OutputNodes[0] = oAndGate.InputNodes[iBit];
                }
            }
            oAndGate.OutputNodes.Connection = Output;
        }
    }
}
